package vn.edu.uit.hotelbooking.JsonParse;

import java.util.ArrayList;

import org.json.JSONArray;

import vn.edu.uit.hotelbooking.model.Country;
import vn.edu.uit.hotelbooking.model.District;
import vn.edu.uit.hotelbooking.model.Province;
import vn.edu.uit.hotelbooking.model.Ward;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class LocationParser {
    private static final String LOG_TAG_COUNTRY_PARSER = "CountryParser";
    private static final String LOG_TAG_PROVINCE_PARSER = "ProvinceParser";
    private static final String LOG_TAG_DISTRICT_PARSER = "DistrictParser";
    private static final String LOG_TAG_WARD_PARSER = "WardParser";
    private static Gson gson = new Gson();
    
    /**
     * The method to get a district arraylist from JSON object
     * @param jObject the JSON object
     * @return If success, return a district arraylist. If fail, return null.
     */
    public static ArrayList<Country> getCountryFromJson (JSONArray jArray) {
        ArrayList<Country> arraylist = new ArrayList<Country>();
        try {
            arraylist = gson.fromJson(jArray.toString(), new TypeToken<ArrayList<Country>>(){}.getType());
             return arraylist;
        } catch (Exception e) {
            // TODO: handle exception
            Log.e(LOG_TAG_COUNTRY_PARSER, e.getMessage());
        }
        return null;
    }
    
    /**
     * The method to get a province arraylist from JSON object
     * @param jObject the JSON object
     * @return If success, return a province arraylist. If fail, return null.
     */
    public static ArrayList<Province> getProvinceFromJson (JSONArray jArray) {
        ArrayList<Province> arrlistProvince = new ArrayList<Province>();
        try {
             arrlistProvince = gson.fromJson(jArray.toString(), new TypeToken<ArrayList<Province>>(){}.getType());
             return arrlistProvince;
        } catch (Exception e) {
            // TODO: handle exception
            Log.e(LOG_TAG_PROVINCE_PARSER, e.getMessage());
        }
        return null;
    }    
    
    /**
     * The method to get a district arraylist from JSON object
     * @param jObject the JSON object
     * @return If success, return a district arraylist. If fail, return null.
     */
    public static ArrayList<District> getDistrictFromJson (JSONArray jArray) {
        ArrayList<District> arrlistDistrict = new ArrayList<District>();
        try {
            arrlistDistrict = gson.fromJson(jArray.toString(), new TypeToken<ArrayList<District>>(){}.getType());
             return arrlistDistrict;
        } catch (Exception e) {
            // TODO: handle exception
            Log.e(LOG_TAG_DISTRICT_PARSER, e.getMessage());
        }
        return null;
    }
    
    /**
     * The method to get a ward arraylist from JSON object
     * @param jObject the JSON object
     * @return If success, return a ward arraylist. If fail, return null.
     */
    public static ArrayList<Ward> getWardFromJson (JSONArray jArray) {
        ArrayList<Ward> arrlistWard = new ArrayList<Ward>();
        try {
            arrlistWard = gson.fromJson(jArray.toString(), new TypeToken<ArrayList<Ward>>(){}.getType());
             return arrlistWard;
        } catch (Exception e) {
            // TODO: handle exception
            Log.e(LOG_TAG_WARD_PARSER, e.getMessage());
        }
        return null;
    }
}
